package com.goktech.sportsmall.service.Impl;

import com.goktech.sportsmall.bean.CartBean;
import com.goktech.sportsmall.mapper.CartMapper;
import com.goktech.sportsmall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired(required = false)
    private CartMapper cartMapper;

    // 添加数据
    @Override
    public int insertCart(CartBean cartBean) {
//         查询数据库中是否存在对应的信息
//        int result = cartMapper.lhwSelectByCsNo(cartBean.getCs_no(),cartBean.getUId());
//        // 如果存在数据，则执行修改数据中的
//        if (result == 1) {
//            return cartMapper.lhwUpdateCart(
//                    cartBean.getCs_no(),
//                    cartBean.getUId(),
//                    (cartBean.getCCount()+cartMapper.lhwSelectInfo(cartBean.getCs_no(),cartBean.getUId()).getCCount()));
//        } else {
//            return cartMapper.insertCart(cartBean);
//        }
        return cartMapper.insertCart(cartBean);

    }

    @Override
    public Map<String, Object> selectList(int page, int size, int uId,String cName,String cSort) {
        Map<String,Object> restMap = new HashMap<>();
        int count = cartMapper.selectCount(uId);
        restMap.put("count",count);//总页数
        restMap.put("page",page);//当前页码
        restMap.put("size",size);//当前每页显示数量
        restMap.put("cartList",cartMapper.selectList((page-1)*size,size,uId,cName,cSort));
        return restMap;
    }

    @Override
    public int updateCount(CartBean cartBean) {
        return cartMapper.updateCount(cartBean);
    }

    @Override
    public int deleteCart(String csNo,int uId) {
        return cartMapper.deleteCart(csNo,uId);
    }
}
